package com.designoutput.ui.controls.rulerComp
{
	import com.designoutput.enum.Direction;
	import com.designoutput.ui.controls.Ruler;
	
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	
	/**
	 * Guide Line to show on the ruler.
	 * 
	 */	
	public class GuideLine extends Sprite
	{
		private var _orientation:Direction ;
		private var _parentRuler:Ruler;
		
		/**
		 * Calibration of guideline when create new one. 
		 * @public
		 */		
		public var calibration:Number; 
		
		public function GuideLine(parentRuler:Ruler)
		{
			_parentRuler = parentRuler;
			addEventListener(MouseEvent.MOUSE_DOWN, lineDown, false, 0, true);
		}
		
		public function get orientation():Direction
		{
			return _orientation;
		}

		public function set orientation(value:Direction):void
		{
			_orientation = value;
			drawLine();
		}
		
		public function get isVertical():Boolean{
			return (orientation == Direction.Vertical);
		}
		
		private function drawLine():void{
			graphics.clear();//0x8491b4
			if(isVertical){
				graphics.lineStyle(5,0xFF0000,0);
				graphics.moveTo(0,0);
				graphics.lineTo(0,3850);
				graphics.lineStyle(1,0x038194,1);
				graphics.moveTo(1,0);
				graphics.lineTo(0,3850);
			}else {
				graphics.lineStyle(5,0xFF0000,0);
				graphics.moveTo(0,0);
				graphics.lineTo(3850,0);
				graphics.lineStyle(1,0x038194,1);
				graphics.moveTo(2,0);
				graphics.lineTo(3850,0);
			}
		}

		public function lineDown(...arg):void{
			if(isVertical){
				startDrag(false, new Rectangle(0,0,3850,0));
			}else{
				startDrag(false, new Rectangle(0,0,0,3850));
			}
			stage.addEventListener(MouseEvent.MOUSE_UP, lineUP, false);
		}
		
		public function lineUP(evt:MouseEvent):void{
			stopDrag();
			
			evt.currentTarget.removeEventListener(MouseEvent.MOUSE_UP, lineUP, false);
			
			if(isVertical){
				if(0 <= x && x <= 10)
					visible = false;

				calibration = (x - 16)/_parentRuler.unitNum;
			}else{
				if(0 <= y && y<=10)
					visible = false;

				calibration = (y - 16)/_parentRuler.unitNum;
			}
		}
	}
}